package com.yami.shop.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${spring.profiles.active}")
    private String profile;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 根据环境区分是否增加跨域信息
        if ("dev".equals(profile) || "test".equals(profile)) {
            registry.addMapping("/**")
                    .allowedOriginPatterns("*")
                    .allowedHeaders("*")
                    .allowCredentials(true)
                    .allowedMethods("*")
                    .maxAge(3600);
        }
    }
}
